package mate.task.odin;

import java.util.*;

/**
 * This class represents a list of to do items.
 * @author Leonard
 * @version 1.0
 */
public class ToDoList {
	private int size;
	private ArrayList<ToDoItem> list;

	/**
	 * Instantiates a new ToDoList object
	 */
	public ToDoList(){
		list = new ArrayList<ToDoItem>();
	}

	/**
	 * Removes item with given name
	 * @param name Name of item to remove
	 */
	public void deleteItem(String name){
		boolean removed = false;
		
		for(int i = 0; i < list.size() && !removed; i++){
			ToDoItem item = list.get(i);
			
			if(item.getName().equals(name)){
				list.remove(item);
				removed = true;
			}
		}
	}
	
	/**
	 * Adds a new item with given values
	 * @param name The name (short description) of the item
	 * @param description The full description of the item
	 * @param tags The tags associated with the item, separated by commas/spaces
	 * @param location The location associated with this item
	 * @param date The due date of this task
	 * @return The created ToDoItem
	 */
	public ToDoItem addItem(String name, String description, String tags, String location, String date) throws Exception{
		ToDoItem item = new ToDoItem(name, description, tags, location, date);
		list.add(item);
		size++;
		return item;
	}

	/**
	 * Returns the list of to do items
	 * @return The list of to do items
	 */
	public ArrayList<ToDoItem> getItemList(){
		return list;
	}


	public int getSize(){
		return size;
	}

	/**
	 * Returns an ArrayList of ToDoItems before given date
	 * @param isComplete The date to filter by
	 * @return An ArrayList of ToDoItems before given date
	 */
	public ArrayList<ToDoItem> filterByDate(String date){
		ArrayList<ToDoItem> temp = new ArrayList<ToDoItem>();

		try{
			Scanner scan = new Scanner(date);
			scan.useDelimiter("/");

			Date filter = new Date(scan.nextInt(),scan.nextInt(),scan.nextInt());

			for(ToDoItem item : list){
				if(item.getDate().compareTo(filter) >= 0){
					temp.add(item);
				}	
			}
		}catch(Exception e){}

		return temp;
	}



	/**
	 * Returns an ArrayList of ToDoItems with given tag
	 * @param tag The tag to filter by
	 * @return An ArrayList of ToDOItems with given tag
	 */
	public ArrayList<ToDoItem> filterByCategory(String tag){
		ArrayList<ToDoItem> temp = new ArrayList<ToDoItem>();

		for(ToDoItem item : list){
			String tags = item.getTags().toLowerCase();
			if(tags.contains(tag.toLowerCase())){
				temp.add(item);
			}
		}

		return temp;
	}

	/**
	 * Returns an ArrayList of ToDoItems with given completion status
	 * @param isComplete The completion status to filter by
	 * @return An ArrayList of ToDOItems with given completion status
	 */
	public ArrayList<ToDoItem> filterByCompletion(boolean isComplete){
		ArrayList<ToDoItem> temp = new ArrayList<ToDoItem>();

		for(ToDoItem item : list){
			if(item.isCompleted() == isComplete){
				temp.add(item);
			}
		}

		return temp;
	}
	
	public ArrayList<ToDoItem> filterByLocation(String loc){
		ArrayList<ToDoItem> temp = new ArrayList<ToDoItem>();
		
		for(ToDoItem item : list){
			if(item.getLocation().toLowerCase().equals(loc.toLowerCase())){
				temp.add(item);
			}
		}
		return temp;
	}

}